﻿Public Class frmMantenimientoMuestra

    Private muestras As New BEmuestras
    Private gestorMuestra As New BLmuestras
    Private edad As Integer
    Private codMedico As Integer
    Private codPaciente As Integer
    Private codDiagnostico As Integer
    Private esModificar As Boolean
    Private codMuestra As Integer
    


    Public Sub cargarDatos()
        Try
            Me.cmbOrgano.DataSource = gestorMuestra.ListarOrganos
            Me.cmbOrgano.DisplayMember = "nomOrg"
            Me.cmbOrgano.ValueMember = "id_organo"
            Me.cmbOrgano.SelectedIndex = 0

            Me.cmbEstado.DataSource = gestorMuestra.ListarEstados
            Me.cmbEstado.DisplayMember = "descripcion"
            Me.cmbEstado.ValueMember = "id_estado"
            Me.cmbEstado.SelectedIndex = 0

            Me.cmbTipMuestra.DataSource = gestorMuestra.ListarTipoMuestras
            Me.cmbTipMuestra.DisplayMember = "nombre"
            Me.cmbTipMuestra.ValueMember = "id_tipo_muestra"
            Me.cmbTipMuestra.SelectedIndex = 0

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    
    Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click



        gestorMuestra.GuardarMuestra(codMuestra, Me._codPaciente, Me.cmbTipMuestra.SelectedValue, Me.cmbOrgano.SelectedValue, Me._codDiagnostico, Me._codMedico, Me.chkTejNormal.Checked, Me.chkVih.Checked, _
                                 Integer.Parse(txtEdad.Text), Me.cmbEstado.SelectedValue, Me.rtbObservacion.Text, Me.txtDescripcion.Text, Me.nupPeso.Value, Me.txtMorfologia.Text, _
                                 Me.nupCantidad.Value, Me.txtTejidoPadre.Text, Me.txtParafinaPadre.Text)
        Me.Close()



    End Sub

    Private Sub btnPaciente_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPaciente.Click
        frmPaciente.ShowDialog()
        codPaciente = frmPaciente._codPaciente
        txtPaciente.Text = frmPaciente._nomPaciente
        txtEdad.Text = ObtenerEdad(frmPaciente._fechaN)
    End Sub

    Private Sub btnDiagnostico_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDiagnostico.Click

        frmDiagnostico.ShowDialog()
        codDiagnostico = frmDiagnostico._codDiagnostico
        txtDiagnostico.Text = frmDiagnostico._nomDiagnostico
    End Sub

    Private Sub btnMedico_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMedico.Click
        frmMedicos.ShowDialog()
        codMedico = frmMedicos._codMedico
        txtMedico.Text = frmMedicos._nomMedico
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click

        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel

        Me.Close()
    End Sub

    Private Sub cmbTipMuestra_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbTipMuestra.SelectedValueChanged

        If (cmbTipMuestra.SelectedIndex = 0) Then
            gpbxTejido.Enabled = False
            gpbxLamina.Enabled = False
            gpbxSangreMO.Enabled = False
            gpbxParafina.Enabled = False
        Else
            If (cmbTipMuestra.SelectedValue = Constantes.TEJIDO) Then

                gpbxTejido.Enabled = True
                gpbxLamina.Enabled = False
                gpbxSangreMO.Enabled = False
                gpbxParafina.Enabled = False

            Else
                If (cmbTipMuestra.SelectedValue = Constantes.SANGRE) Then
                    gpbxTejido.Enabled = False
                    gpbxLamina.Enabled = False
                    gpbxSangreMO.Enabled = True
                    gpbxParafina.Enabled = False
                Else
                    If (cmbTipMuestra.SelectedValue = Constantes.PARAFINA) Then
                        gpbxTejido.Enabled = False
                        gpbxLamina.Enabled = False
                        gpbxSangreMO.Enabled = False
                        gpbxParafina.Enabled = True

                    Else
                        If (cmbTipMuestra.SelectedValue = Constantes.LAMINA) Then
                            gpbxTejido.Enabled = False
                            gpbxLamina.Enabled = True
                            gpbxSangreMO.Enabled = False
                            gpbxParafina.Enabled = False
                        End If
                    End If
                End If
            End If

        End If
    End Sub

    Private Sub frmMantenimientoMuestra_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cargarDatos()

        If (Me.esModificar = True) Then
            Me.gestorMuestra.buscarMuestraMod(Me.codMuestra, Me)
        End If

    End Sub

    Public Property _codMedico()
        Get
            Return Me.codMedico
        End Get
        Set(ByVal value)

            Me.codMedico = value
        End Set
    End Property

    Public Property _codPaciente()
        Get
            Return Me.codPaciente
        End Get
        Set(ByVal value)

            Me.codPaciente = value
        End Set
    End Property

    Public Property _codDiagnostico()
        Get
            Return Me.codDiagnostico
        End Get
        Set(ByVal value)
            Me.codDiagnostico = value
        End Set
    End Property

    Public Property _esModificar()
        Get
            Return Me.esModificar
        End Get
        Set(ByVal value)
            Me.esModificar = value
        End Set
    End Property

    Public Property _codMuestra()
        Get
            Return Me.codMuestra
        End Get
        Set(ByVal value)
            Me.codMuestra = value
        End Set
    End Property

    Public Function ObtenerEdad(ByVal fecha As Date)
        Dim aEdad As Integer = Date.Today.Year - fecha.Year
        If (Date.Today.Month = fecha.Month) Then
            If (Date.Today.Day < fecha.Day) Then
                edad = aEdad - 1
            End If
        Else
            If (Date.Today.Month < fecha.Month) Then
                edad = aEdad - 1
            End If
        End If
        edad = aEdad
        Return edad
    End Function


End Class